from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QVBoxLayout, QHBoxLayout, QTableWidget,
                             QPushButton, QLabel, QComboBox, QSpinBox)


class BasePaginationView:
    """分页视图基类，包含通用分页逻辑"""
    def __init__(self, parent=None):
        self.parent = parent
        self.table = None
        self.current_page = 1
        self.total_pages = 1
        self.page_size = 10
        self.book_id = None

        # 分页控件
        self.prev_btn = None
        self.next_btn = None
        self.page_label = None
        self.total_pages_label = None
        self.page_size_combo = None
        self.page_spin = None

    def init_pagination_controls(self, layout):
        """初始化分页控制组件"""
        pagination_layout = QHBoxLayout()
        pagination_layout.setContentsMargins(0, 10, 0, 0)

        # 上一页按钮
        self.prev_btn = QPushButton("上一页")
        self.prev_btn.clicked.connect(self.go_to_prev_page)
        self.prev_btn.setEnabled(True)
        self.prev_btn.setProperty('class', 'theme-text theme-aware')
        pagination_layout.addWidget(self.prev_btn)

        # 页码显示
        self.page_label = QLabel("第1页")
        self.page_label.setProperty('class', 'theme-text theme-aware')
        self.page_label.setStyleSheet("""
            QLabel {
                color: palette(windowText);
            }
        """)
        pagination_layout.addWidget(self.page_label)

        # 下一页按钮
        self.next_btn = QPushButton("下一页")
        self.next_btn.clicked.connect(self.go_to_next_page)
        self.next_btn.setEnabled(False)
        self.next_btn.setProperty('class', 'theme-text theme-aware')
        pagination_layout.addWidget(self.next_btn)

        # 每页显示数量选择
        page_size_label = QLabel("每页显示:")
        page_size_label.setProperty('class', 'theme-text theme-aware')
        page_size_label.setStyleSheet("""
            QLabel {
                color: palette(windowText);
            }
        """)
        pagination_layout.addWidget(page_size_label)

        self.page_size_combo = QComboBox()
        self.page_size_combo.setProperty('class', 'theme-combo theme-aware')
        self.page_size_combo.addItems(["10", "20", "50", "100"])
        self.page_size_combo.setCurrentIndex(0)
        self.page_size_combo.currentTextChanged.connect(self.change_page_size)
        self.page_size_combo.setStyleSheet("""
            QComboBox {
                color: palette(windowText);
                background: palette(base);
            }
        """)
        pagination_layout.addWidget(self.page_size_combo)

        # 总页数显示
        self.total_pages_label = QLabel("共1页")
        self.total_pages_label.setProperty('class', 'theme-text theme-aware')
        self.total_pages_label.setStyleSheet("""
            QLabel {
                color: palette(windowText);
            }
        """)
        pagination_layout.addWidget(self.total_pages_label)

        # 页码跳转
        goto_label = QLabel("跳转到:")
        goto_label.setProperty('class', 'theme-text theme-aware')
        goto_label.setStyleSheet("""
            QLabel {
                color: palette(windowText);
            }
        """)
        pagination_layout.addWidget(goto_label)

        self.page_spin = QSpinBox()
        self.page_spin.setProperty('class', 'theme-spin theme-aware')
        self.page_spin.setMinimum(1)
        self.page_spin.setMaximum(1)
        self.page_spin.valueChanged.connect(self.go_to_page)
        self.page_spin.setStyleSheet("""
            QSpinBox {
                color: palette(windowText);
                background: palette(base);
            }
        """)
        pagination_layout.addWidget(self.page_spin)

        layout.addLayout(pagination_layout)

    def update_pagination_controls(self):
        """更新分页控制组件状态"""
        self.page_label.setText(f"第{self.current_page}页")
        self.total_pages_label.setText(f"共{self.total_pages}页")
        self.page_spin.setMaximum(self.total_pages)
        self.page_spin.setValue(self.current_page)

        # 更新按钮状态
        prev_enabled = self.current_page > 1
        next_enabled = self.current_page < self.total_pages

        self.prev_btn.setEnabled(prev_enabled)
        self.next_btn.setEnabled(next_enabled)

    def go_to_prev_page(self):
        """跳转到上一页"""
        if self.current_page > 1:
            self.current_page -= 1
            self.fetch_data()

    def go_to_next_page(self):
        """跳转到下一页"""
        if self.current_page < self.total_pages:
            self.current_page += 1
            self.fetch_data()

    def go_to_page(self, page):
        """跳转到指定页码"""
        if 1 <= page <= self.total_pages and page != self.current_page:
            self.current_page = page
            self.fetch_data()

    def change_page_size(self, size):
        """更改每页显示数量"""
        self.page_size = int(size)
        self.current_page = 1
        self.fetch_data()

    def fetch_data(self):
        """抽象方法，子类需实现"""
        raise NotImplementedError("子类必须实现fetch_data方法")